package cn.gong.jizhang.datacenter.db;

import java.util.ArrayList;

import cn.gong.jizhang.model.Record;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class RecordDb extends BaseDb {
	// record(id INTEGER primary key,add_time integer, count integer,mark
	// VARCHAR(100)
	private static final int DEFAULT_COUNT = 20;

	public void addRecord(int money, String mark) {
		SQLiteDatabase db = getDb();
		db.execSQL("insert into record(add_time,count,mark) values(?,?,?)", new Object[] { System.currentTimeMillis(), money, mark });
		closeDb(db);
		db = null;
	}

	public ArrayList<Record> getRecordList(int page) {
		SQLiteDatabase db = getDb();
		Cursor cursor = db.rawQuery("select * from record order by id desc limit ?,?", new String[] { String.valueOf((page - 1) * DEFAULT_COUNT), String.valueOf(DEFAULT_COUNT) });
		ArrayList<Record> records = new ArrayList<Record>();
		while (cursor.moveToNext()) {
			Record record = new Record();
			record.setId(cursor.getLong(cursor.getColumnIndex("id")));
			record.setCount(cursor.getInt(cursor.getColumnIndex("count")));
			record.setAddTime(cursor.getLong(cursor.getColumnIndex("add_time")));
			record.setMark(cursor.getString(cursor.getColumnIndex("mark")));
			records.add(record);
		}
		return records;
	}
}
